//
//  ThirdLoginView.swift
//  maopu
//
//  Created by huazi on 2017/5/1.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit
protocol ThirdLoginViewDelegate: NSObjectProtocol {
    func thirdLoginViewitemClick(index: Int)
}

class ThirdLoginView: UIView {
    fileprivate var viewLine: UIView!
    fileprivate var labelLoginWay: UILabel!
    weak var delegate: ThirdLoginViewDelegate?
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initView(){
        self.viewLine = UIView.init(frame: CGRect.init(x: 60, y: 20, width: AppWidth-120, height: 0.5))
        self.viewLine.backgroundColor = UIColor.gray
        self.addSubview(self.viewLine)
        
        self.labelLoginWay = UILabel.init(frame: CGRect.init(x: AppWidth/2 - 35, y: 10, width: 70, height: 20))
        self.labelLoginWay.backgroundColor = UIColor.m_viewBackgroundColor()
        self.labelLoginWay.textColor = UIColor.m_textLightColor()
        self.labelLoginWay.font = UIFont.LightDefaultFont(9)
        self.labelLoginWay.textAlignment = .center
        self.labelLoginWay.text = "其它登录方式"
        self.addSubview(self.labelLoginWay)
        
        let thirdLoginItemQQ: ThirdLoginItemView = ThirdLoginItemView.init(title: "QQ", imageName: "share_QQ", frame: CGRect.init(x: AppWidth/2-30 - 80, y: 45, width: 60, height: 100))
        thirdLoginItemQQ.tag = 100
        thirdLoginItemQQ.btnLoginClickClosure = {(btn) in
            self.delegate?.thirdLoginViewitemClick(index: 100)
        }
        self.addSubview(thirdLoginItemQQ)
        
        let thirdLoginItemWx: ThirdLoginItemView = ThirdLoginItemView.init(title: "微信", imageName: "share_weChat", frame: CGRect.init(x: AppWidth/2-30, y: 45, width: 60, height: 100))
        thirdLoginItemWx.tag = 101
        thirdLoginItemWx.btnLoginClickClosure = {(btn) in
            self.delegate?.thirdLoginViewitemClick(index: 101)
        }
        self.addSubview(thirdLoginItemWx)
        
        let thirdLoginItemWb: ThirdLoginItemView = ThirdLoginItemView.init(title: "微博", imageName: "share_weibo", frame: CGRect.init(x: AppWidth/2-30 + 80, y: 45, width: 60, height: 100))
        thirdLoginItemWb.tag = 102
        thirdLoginItemWb.btnLoginClickClosure = {(btn) in
            self.delegate?.thirdLoginViewitemClick(index: 102)
        }
        self.addSubview(thirdLoginItemWb)
        
        let arrInstalledPlat: NSMutableArray = NSMutableArray.init(array: ["1","1","1"])
        if !UIApplication.shared.canOpenURL(URL.init(string: "mqq://")!) {
            arrInstalledPlat.replaceObject(at: 0, with: "0")
            thirdLoginItemQQ.isHidden = true
        }
        if !UIApplication.shared.canOpenURL(URL.init(string: "wechat://")!) {
            arrInstalledPlat.replaceObject(at: 1, with: "0")
            thirdLoginItemWx.isHidden = true
        }
        if !UIApplication.shared.canOpenURL(URL.init(string: "sinaweibo://")!) {
            arrInstalledPlat.replaceObject(at: 2, with: "0")
            thirdLoginItemWb.isHidden = true
        }

        var installedCount: Int8 = 0
        for index in 0...2 {
            if Int(arrInstalledPlat[index] as! String) == 1 {
                installedCount += 1
            }
        }
        if (installedCount == 0) {
            self.isHidden = true
            return ;
        }
        if (installedCount == 3) {
            return ;
        }
        if installedCount == 1 {
            for index in 0...2 {
                if Int(arrInstalledPlat[index] as! String) == 1 {
                    let item: ThirdLoginItemView = self.viewWithTag(100 + index) as! ThirdLoginItemView as ThirdLoginItemView
                    item.frame = CGRect.init(x: AppWidth/2-30, y: 45, width: 60, height: 100)
                }

            }
        }
        if installedCount == 2 {
            var isSecond: Bool = false
            for index in 0...2 {
                if Int(arrInstalledPlat[index] as! String) == 1 {
                    if isSecond {
                        let item: ThirdLoginItemView = self.viewWithTag(100 + index) as! ThirdLoginItemView as ThirdLoginItemView
                        item.frame = CGRect.init(x: AppWidth/2-30 + 60, y: 45, width: 60, height: 100)
                    }
                    else {
                        let item: ThirdLoginItemView = self.viewWithTag(100 + index) as! ThirdLoginItemView as ThirdLoginItemView
                        item.frame = CGRect.init(x: AppWidth/2-30 - 60, y: 45, width: 60, height: 100)
                        isSecond = true
                    }
                }
                
            }
        }
    }    
}
